package cn.gdcp.lkqapp08;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private Button btnJSON;
    private Button btnOneJSON;
    private Button btnJSONArray;
    private Button btnCity;
    private TextView txtResult;
    private String TAG = "MainActivity";

    private String jsonStr = "[\n" +
            "{\n" +
            "\"id\": \"1\",\n" +
            "\"name\": \"Lisa\",\n" +
            "\"age\": \"22\"\n" +
            "},\n" +
            "{\n" +
            "\"id\": \"2\",\n" +
            "\"name\": \"Ben\",\n" +
            "\"age\": \"23\"\n" +
            "},\n" +
            "{\n" +
            "\"id\": \"3\",\n" +
            "\"name\": \"Tom\",\n" +
            "\"age\": \"24\"\n" +
            "}\n" +
            "]";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnJSON = findViewById(R.id.btn1);
        btnOneJSON = findViewById(R.id.btn2);
        btnJSONArray = findViewById(R.id.btn3);
        btnCity = findViewById(R.id.btn4);
        txtResult = findViewById(R.id.txt_result);

        btnJSON.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    JSONArray jsonArray = new JSONArray(jsonStr);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        int no = jsonObject.getInt("id");
                        String name = jsonObject.getString("name");
                        int age = jsonObject.getInt("age");
                        Log.e(TAG, " no: " + no + ",name: " + name + ",age: " + age);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });


        final Student s1 = new Student(11, "渣渣辉", 38);
        final Student s2 = new Student(12, "古天乐", 37);
        final Student s3 = new Student(13, "陈小春", 39);
        final ArrayList<Student> studentArrayList = new ArrayList<>();
        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);

        btnOneJSON.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("stuno", s1.getStuno());
                    jsonObject.put("name", s1.getName());
                    jsonObject.put("age", s1.getAge());
                    Log.e(TAG, "jsonObject = " + jsonObject.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

        btnJSONArray.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    JSONArray jsonArray = new JSONArray();
                    for (Student student : studentArrayList) {
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("stuno", student.getStuno());
                        jsonObject.put("name", student.getName());
                        jsonObject.put("age", student.getAge());
                        jsonArray.put(jsonObject);
                    }
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("stuList",jsonArray);
                    Log.e(TAG,"jsonObject = " + jsonObject.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });

        btnCity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,ThreeLevelCity.class);
                startActivityForResult(intent,1001);
            }
        });


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if(resultCode == 2001){
            String result = data.getStringExtra("cityarea");
            txtResult.setText("城市地区：" + result);
        }
    }
}
